﻿package frEngine.animateControler.particleControler
{
    import __AS3__.vec.*;
    import baseEngine.core.*;
    import baseEngine.modifiers.*;
    import flash.events.*;
    import flash.geom.*;
    import frEngine.*;
    import frEngine.animateControler.keyframe.*;
    import frEngine.effectEditTool.lineRoad.*;
    import frEngine.event.*;

    public class LineRoadControler extends Modifier
    {
        private var _lineRoadMesh3d:LineRoadMesh3D;
        private var _useRot:Boolean;
        private static var _tempArr:Vector.<Vector3D> = new Vector.<Vector3D>(100, true);

        public function LineRoadControler()
        {
            return;
        }// end function

        override public function dispose(param1:Boolean = true) : void
        {
            super.dispose();
            if (this._lineRoadMesh3d)
            {
                FrEventDispatcher.instance.proxyRemoveEventListener(this._lineRoadMesh3d, Engine3dEventName.UPDATE_TRANSFORM_EVENT, this.updateCacheHander);
            }
            this._lineRoadMesh3d = null;
            return;
        }// end function

        public function init(param1:LineRoadMesh3D, param2:Boolean) : void
        {
            if (this._lineRoadMesh3d)
            {
                FrEventDispatcher.instance.proxyRemoveEventListener(this._lineRoadMesh3d, Engine3dEventName.UPDATE_TRANSFORM_EVENT, this.updateCacheHander);
            }
            this._lineRoadMesh3d = param1;
            this._useRot = param2;
            if (_cache[currentFrame] != null)
            {
                this.setTargetProperty(_cache[currentFrame]);
            }
            if (this._lineRoadMesh3d)
            {
                FrEventDispatcher.instance.proxyAddEventListener(this._lineRoadMesh3d, Engine3dEventName.UPDATE_TRANSFORM_EVENT, this.updateCacheHander);
            }
            targetObject3d.parent = this._lineRoadMesh3d;
            return;
        }// end function

        private function updateCacheHander(event:Event) : void
        {
            this._lineRoadMesh3d.clearCache(this._useRot);
            if (_cache[currentFrame] != null)
            {
                this.setTargetProperty(_cache[currentFrame]);
            }
            return;
        }// end function

        override public function clearCache() : void
        {
            super.clearCache();
            targetObject3d.transform.identity();
            targetObject3d.setTransform(targetObject3d.transform, false);
            return;
        }// end function

        override public function get type() : int
        {
            return AnimateControlerType.LineRoadControler;
        }// end function

        override protected function getBaseValue(param1:Pivot3D) : Object
        {
            return 0;
        }// end function

        override protected function setTargetProperty(param1) : void
        {
            var _loc_2:Matrix3D = null;
            var _loc_3:Vector3D = null;
            if (this._lineRoadMesh3d)
            {
                if (this._useRot)
                {
                    _loc_2 = this._lineRoadMesh3d.getMatrix3dByTime(param1);
                    targetObject3d.setTransform(_loc_2, false);
                }
                else
                {
                    _loc_3 = this._lineRoadMesh3d.getPosByTime(param1);
                    targetObject3d.setPosition(_loc_3.x, _loc_3.y, _loc_3.z);
                }
            }
            return;
        }// end function

        public function getMidPos(param1:int) : Vector.<Vector3D>
        {
            var _loc_2:int = 0;
            var _loc_3:Number = NaN;
            var _loc_4:Number = NaN;
            var _loc_5:Number = NaN;
            var _loc_6:Number = NaN;
            var _loc_7:int = 0;
            if (this._lineRoadMesh3d)
            {
                _loc_2 = this.currentFrame - 1;
                var _loc_8:int = 0;
                _loc_2 = 0;
                if (!_cache[this.currentFrame])
                {
                    calculateFrameValue(this.currentFrame);
                }
                _loc_3 = _cache[this.currentFrame];
                if (!_cache[_loc_2])
                {
                    calculateFrameValue(_loc_2);
                }
                _loc_4 = _cache[_loc_2];
                _loc_5 = _loc_3 - _loc_4;
                _loc_5 = _loc_5 + 1;
                _loc_6 = _loc_5 / param1;
                _loc_7 = 0;
                while (_loc_7 < param1)
                {
                    
                    _loc_4 = _loc_4 + _loc_6;
                    var _loc_8:int = 1;
                    _loc_4 = 1;
                    _tempArr[_loc_7] = this._lineRoadMesh3d.getPosByTime(_loc_4);
                    _loc_7++;
                }
            }
            return _tempArr;
        }// end function

    }
}
